//
//  ViewController.m
//  04 - UIControl
//
//  Created by GivenBmli on 16/7/19.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //  1 . UISwith （开关）
    self.view.backgroundColor = [UIColor brownColor];
    //    Swith 宽和高不影响控件的大小, 只有x，y
    UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 200, 300)];
    swith.backgroundColor = [UIColor clearColor];
    //    开启时的控件内部颜色变了
    swith.onTintColor = [UIColor blackColor];
    //    按钮的边框的颜色 及切换时到关闭时的颜色变了(null_resettable)
    swith.tintColor = [UIColor cyanColor];
    //    中间按钮的颜色
    //    3. 开关按钮（圆圈）的颜色
    swith.thumbTintColor = [UIColor blueColor];
    //    iOS七后无效果
    swith.onImage = [UIImage imageNamed:@"20151116102246252"];
    //    关闭时的图片效果 iOS七后消失
    swith.offImage = [UIImage imageNamed:@"21c91a385343fbf221fa73aeb07eca8067388ffb"];
    //    判断swithControl的开闭状态
    //    NO 为关闭状态
    //    通过setter改变swithControl的初始值变化
    //    swith.on = YES;
    [self.view addSubview:swith];
    NSLog(@"%@", swith.isOn ? @"YES" : @"NO");
    [swith setOn:YES animated:YES];
    //    7. 添加绑定事件(与button相同)
    [swith addTarget:self action:@selector(swith:) forControlEvents:UIControlEventValueChanged];
    
    //    2. slder
    //    slider 高度不影响控件的高度， 这个控件用于居中于视图中间
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 200, 200, 50)];
    
    //   1.  slider
    //    当前滑块的值
    slider.value = 0;
    //    2. 滑块最小值
    slider.minimumValue = 0;
    //    滑块最大值
    slider.maximumValue = 1;
    //    以上最大和最可以根据需求去改变
    //    4 minmumValueImage最小那边显示的图片
    //    5 maximumValueImage最大那边显示的图片
    //    6
    //    7.绑定事件
    [slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
    
    //    8.maximumTrackTintColor 未移动到的区域颜色
    //     minimumTrackTintColor 已经移动到的区域颜色
    //    slider.maximumTrackTintColor = [UIColor cyanColor];
    //    slider.minimumTrackTintColor = [UIColor blueColor];
    //    滑动最小值
    slider.minimumValue = 0;
    //    滑动最大值
    slider.maximumValue = 1;
    //    按钮添加方法
    [slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
    //    按钮的中心点位置
    slider.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height * 0.3);
    //    9 .thumbTintColor 滑块的颜色
    //    slider.thumbTintColor = [UIColor orangeColor];
    //    slider.tintColor = [UIColor blackColor];
    //    slider.backgroundColor = [UIColor redColor];
    //    10设置进度条滑块右边的图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    //    左
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    //    13 设置滑块颜色
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [self.view addSubview:slider];
    //        slider.continuous = NO;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 110)];
    btn.backgroundColor = [UIColor purpleColor];
    [btn addTarget:self action:@selector(segment:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //    3. UISegmentedControl
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"first", @"secont", @"lastttttttt"]];
    segment.frame = CGRectMake(0, 10, 220, 20);
    //    segment 不给frame, 系统会自动计算大小（推荐：加上frame）
    segment.center = CGPointMake(self.view.frame.size.width / 2, 30);
    //   1.Momentary
    //    如果选中设置为YES, 如果点击选中没有选中的效果 默认为NO
    segment.momentary = YES;
    //    2numberOfSegments
    //    显示有多少个段
    NSInteger numofsegment = segment.numberOfSegments;
    NSLog(@"%ld", numofsegment);
    //    NSLog(@"%@", NSStringFromCGRect(segment.frame));
    segment.backgroundColor = [UIColor redColor];
    segment.tag = 1000;
    //   添加方法
    //    insertSegmentWithTitle
    //   移除方法
    //    removeAllSegments
    //    5.setTitle:forSegmentAtIndex:用来修改对应标题
    //    设置0自己计算
    [segment setWidth:90 forSegmentAtIndex:0];
    //    6.setWidth:forSegmentAtIndex width：默认是0.0
    [segment setTitle:@"啊蔚" forSegmentAtIndex:0];
    
    //    7. 设置对应下表内容的偏移
    [segment setContentOffset:CGSizeMake(0, 0) forSegmentAtIndex:0];
    //    8. enable 是否能被选中
    [segment setEnabled:YES forSegmentAtIndex:0];
    //       9. selectedSegmentIndex 选中的下标
    segment.selectedSegmentIndex = 1;
    //    10. titnColor文字及边框颜色
    [self.view addSubview:segment];
    //    11. apportionsSegmentWidthsByContent 默认为no
    //    如果分段控制器的段宽为0时, 并且该属性为YES, 系统会自动计算
    segment.apportionsSegmentWidthsByContent = YES;
//    segement 绑定事件
    [segment addTarget:segment action:@selector(seg:) forControlEvents:UIControlEventValueChanged];
//    UIPageControl
    UIPageControl *pag = [[UIPageControl alloc]initWithFrame:CGRectMake(20, 400, 200, 50)];
//    1分页控制器 总页数 默认为0
    pag.numberOfPages = 6;
//    2当前页数
    pag.currentPage = 3;
//    3当只有一页时隐藏
    pag.hidesForSinglePage = YES;
//    4获取到点的大小
CGSize size = [pag sizeForNumberOfPages:20];
    NSLog(@"%@",NSStringFromCGSize(size));
//    5当前页的颜色
    pag.pageIndicatorTintColor = [UIColor greenColor];
//    6选中也的颜色
    pag.currentPageIndicatorTintColor = [UIColor redColor];
    [self.view addSubview:pag];
//   7. 绑定事件
    [pag addTarget:self action:@selector(pagControllChange:) forControlEvents:UIControlEventValueChanged];
//    8.关闭系统事件page的切换 直到调用updateCurrentPageDisplay方法才会显示
    pag.defersCurrentPageDisplay = YES;

 }

- (void)pagControllChange:(UIPageControl *)pagControllChange {
    [pagControllChange updateCurrentPageDisplay];
    NSLog(@"changfefe");
}


- (void)segment:(UIButton *)btn {
    UISegmentedControl *seg = [self.view viewWithTag:1000];
    [seg insertSegmentWithTitle:@"张合蔚" atIndex:0 animated:YES];
}
- (void)slider:(UISlider *)slider {
    NSLog(@"%f", slider.value);
}
- (void)seg:(UISegmentedControl *)seg {
    NSLog(@"%ld", seg.selectedSegmentIndex);
    // 根据下标来判断 if else
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor redColor];
            break;
        case 1:
            self.view.backgroundColor = [UIColor yellowColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor greenColor];
            break;
        default:
            break;
    }
}

//swith触发的事件
- (void)swith:(UISwitch *)swith {
    //    根据swith的状态, 决定触发生什么样的事件
    if (swith.on == YES) {
        NSLog(@"开");
    } else {
        NSLog(@"关");
    }
}- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
